// 这道题, 使用普通的bfs也可以解决问题, 不过时间复杂度较高, 并且题目中,没有界定m,n的
// 大小范文, 并且这是一道高分题, 所以建议使用最优的算法, 考虑时间复杂度,
// 其实题目不难, 只是代码量稍多

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let m, n; //m 行, n列
let initArr = [];

class MinTopHeap {
    constructor() {
        this.heap = []
    }
    pickUp(){
        if(this.heap.length === 1 || this.heap.length === 1) {
            return this.heap.pop()
        }
        let temp = this.heap[0]
        this.heap[0] = this.heap.pop()
        this.downSink()
        return temp;

    }
    push(x){
        this.heap.push(x)
        this.upBubble()
    }

    // 下沉
    downSink(){
        let index = 0;
        while(true){
            let left = 2 * index + 1
            let right = 2 * index + 2
            let min = index;
            if(left < this.heap.length && this.heap[left].value < this.heap[min].value){
                min = left
            }
            if(right < this.heap.length && this.heap[right].value < this.heap[min].value){
                min = right
            }
            if(min === index) break
            [this.heap[index], this.heap[min]] = [this.heap[min], this.heap[index]]
            index = min
        }
    }

    //上浮
    upBubble(){
        let index = this.heap.length - 1
        while(true){
            let parent = Math.floor((index - 1)/2)
            if(parent >= 0 && this.heap[index].value < this.heap[parent].value) {
                [this.heap[parent].value, this.heap[index].value] =  [this.heap[index].value, this.heap[parent].value]
                index = parent
            } else {
                break
            }
        }
    }
}


rl.on("line", (line)=>{
    if(m === undefined) {
        [m, n] = line.trim().split(" ").map(Number)
    } else {
        initArr.push(line.trim().split(" ").map(Number))
        if(initArr.length === m) {

            console.log(getResult(m, n, initArr))
            // clear
            m = n = undefined
            initArr.length = 0
        }
    }

})

function getResult(m, n, initArr) {
    let dp = new Array(m).fill(null).map(_ => new Array(m).fill(Infinity))
    let direction = [
        [-1, 0],
        [1, 0],
        [0, -1],
        [0, 1],
        [-1, -1],
        [-1, 1],
        [1, -1],
        [1, 1]
    ]
    dp[0][0] = initArr[0][0];
    let pq = new MinTopHeap()
    pq.push({x: 0, y: 0, number: initArr[0][0], value: initArr[0][0]})

    while(pq.heap.length) {
        let {x, y, number, value} = pq.pickUp()
        if(dp[x][y] < value) continue

        for (const [addX, addY] of direction) {
            let newX = x + addX
            let newY = y + addY
            if(newX >= 0 && newX < m && newY > 0 && newY < n ) {
                let temp = value + initArr[newX][newY]
                if(initArr[newX][newY] === number) {
                    temp--
                }

                if(temp < dp[newX][newY]) {
                    pq.push({
                        x: newX,
                        y: newY,
                        number: initArr[newX][newY],
                        value: temp
                    })
                    dp[newX][newY] = temp
                }
            }

        }
    }
    return dp[m - 1][n - 1]
}

